package com.ds.bpm.engine.subflow.db;

import com.ds.bpm.engine.BPMConstants;
import com.ds.bpm.engine.database.hib.BaseHibernateDAO;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class DbActRefPdDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(BPMConstants.CONFIG_KEY, DbActRefPdDAO.class);

    // property constants
    public static final String ACTIVITYDEFID = "activitydefId";
    public static final String PROCESSTYPE = "processtype";
    public static final String ISWAITRETURN = "iswaitreturn";

    public static final String MAINPROCESS_VERID = "mainprocessVerId";
    public static final String PARENTPROCESS_VERID = "parentprocessVerId";
    public static final String DESTPROCESS_VERID = "destprocessVerId";

    public void update(ActRefPd transientInstance) {
        log.debug("saving ActRefPd instance");
        try {
            getSession().getTransaction().begin();
            getSession().update(transientInstance);
            getSession().getTransaction().commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        } finally {
            close();
        }
    }

    public void delete(ActRefPd dbActRefPd) {
        log.debug("deleting ActRefPd instance");
        try {
            getSession().getTransaction().begin();

            getSession().delete(dbActRefPd);
            getSession().getTransaction().commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        } finally {
            close();
        }
    }

    public ActRefPd findById(String id) {
        log.debug("getting ActRefPd instance with id: " + id);
        ActRefPd instance = null;
        try {
            instance = (ActRefPd) getSession().get("com.ds.bpm.engine.subflow.db.ActRefPd", id);



        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        } finally {
            close();
        }
        return instance;
    }

    public List<ActRefPd> findByExample(ActRefPd instance) {
        log.debug("finding ActRefPd instance by example");
        List queryList = null;
        try {
            queryList = getSession().createCriteria("com.ds.bpm.engine.subflow.db.ActRefPd").add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + queryList.size());

        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        } finally {
            close();
        }
        return queryList;
    }

    public List<ActRefPd> findByProperty(String propertyName, Object value) {
        log.debug("finding ActRefPd instance with property: " + propertyName + ", value: " + value);
        List<ActRefPd> queryList = null;
        try {
            Criteria criteria = getSession().createCriteria("com.ds.bpm.engine.subflow.db.ActRefPd");
            criteria.add(Restrictions.eq(propertyName, value));
            queryList = criteria.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        } finally {
            close();
        }
        return queryList;
    }

    public List<ActRefPd> findAll() {
        List queryList = null;
        log.debug("finding all ActRefPd instances");
        try {
            Criteria criteria = getSession().createCriteria("com.ds.bpm.engine.subflow.db.ActRefPd");
            queryList = criteria.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        } finally {
            close();
        }
        return queryList;
    }

    public void insert(ActRefPd instance) {
        log.debug("insert ActRefPd instance");
        try {
            getSession().getTransaction().begin();
            getSession().insert(instance);
            getSession().getTransaction().commit();
            log.debug("insert successful");
        } catch (RuntimeException re) {
            log.error("insert failed", re);
            throw re;
        } finally {
            close();
        }
    }

    public List<ActRefPd> findByactivitydefId(String activitydefId) {
        return findByProperty(ACTIVITYDEFID, activitydefId);
    }

    public List<ActRefPd> findByprocesstype(String processtype) {
        return findByProperty(PROCESSTYPE, processtype);
    }

    public List<ActRefPd> findByiswaitreturn(String iswaitreturn) {
        return findByProperty(ISWAITRETURN, iswaitreturn);
    }

    public List<ActRefPd> findBymainprocessVerId(String mainprocessVerId) {
        return findByProperty(MAINPROCESS_VERID, mainprocessVerId);
    }

    public List<ActRefPd> findByparentprocessVerId(String parentprocessVerId) {
        return findByProperty(PARENTPROCESS_VERID, parentprocessVerId);
    }

    public List<ActRefPd> findBydestprocessVerId(String destprocessVerId) {
        return findByProperty(DESTPROCESS_VERID, destprocessVerId);
    }

}